﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AutoWelding.Models;

using MySql.Data.MySqlClient;

namespace AutoWelding.DAL
{

    public class ProductsDAL
    {
        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="isDeleteCode"></param>
        /// <returns></returns>
        public List<ProductsModel> GetProductsList(string productName, int isDeleteCode)
        {
            string selectsSql = "select * from products where 1=1";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            if (!string.IsNullOrEmpty(productName) || productName != "")
            {
                selectsSql += " and product_name like @ProductName";
                parameters.Add(new MySqlParameter("@ProductName", MySqlDbType.VarChar) { Value = "%" + productName + "%" });
            }
            selectsSql += " and is_delete = @IsDelete order by create_time desc";
            parameters.Add(new MySqlParameter("@IsDelete", MySqlDbType.Int32) { Value = isDeleteCode });

            Console.Write("查询产品列表的语句:" + selectsSql);

            return MysqlHelp.ExecuteQuery<ProductsModel>(selectsSql,
                reader => new ProductsModel
                {
                    ProductId = Convert.ToInt32(reader["product_id"]),
                    ProductName = reader["product_name"].ToString(),
                    ProductSku = reader["product_sku"].ToString(),
                    CreateTime = Convert.ToDateTime(reader["create_time"]),
                    //UpdateTime = Convert.ToDateTime(reader["update_time"]),
                    Remark = reader["remark"].ToString()
                }, parameters.ToArray());
        }

        /// <summary>
        /// 根据产品Id获取产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ProductsModel GetProductById(int productId)
        {
            string selectSql = "select * from products where product_id = @ProductId";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@ProductId", MySqlDbType.Int32) { Value = productId });
            // 使用 ExecuteQuery 方法获取 DataTable
            DataTable dataTable = MysqlHelp.ExecuteQuery(selectSql, parameters.ToArray());
            // 检查是否返回了数据
            if (dataTable.Rows.Count > 0)
            {
                // 从 DataTable 中提取数据并映射到 ProductsModel 对象
                DataRow row = dataTable.Rows[0];
                ProductsModel model = new ProductsModel
                {
                    ProductId = Convert.ToInt32(row["product_id"]),
                    ProductName = row["product_name"].ToString(),
                    ProductSku = row["product_sku"].ToString(),
                    CreateTime = Convert.ToDateTime(row["create_time"]),
                    //UpdateTime = Convert.ToDateTime(row["update_time"]),
                    Remark = row["remark"].ToString()
                };
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
